home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / cpptutor.arc / OBJLINK.CPP < prev    next >
Text File  |  1991-04-28  |  2KB  |  103 lines

  1.                                  // Chapter 6 - Program 6
  2. #include "iostream.h"
  3.  
  4. class box {
  5.    int length;
  6.    int width;
  7.    box *another_box;
  8. public:
  9.    box(void);             //Constructor
  10.    void set(int new_length, int new_width);
  11.    int get_area(void);
  12.    void point_at_next(box *where_to_point);
  13.    box *get_next(void);
  14. };
  15.  
  16.  
  17. box::box(void)        //Constructor implementation
  18. {
  19.    length = 8;
  20.    width = 8;
  21.    another_box = NULL;
  22. }
  23.  
  24.  
  25. // This method will set a box size to the two input parameters
  26. void box::set(int new_length, int new_width)
  27. {
  28.    length = new_length;
  29.    width = new_width;
  30. }
  31.  
  32.  
  33. // This method will calculate and return the area of a box instance
  34. int box::get_area(void)
  35. {
  36.    return (length * width);
  37. }
  38.  
  39.  
  40. // This method causes the pointer to point to the input parameter
  41. void box::point_at_next(box *where_to_point)
  42. {
  43.    another_box = where_to_point;
  44. }
  45.  
  46.  
  47. // This method returns the box that this one points to
  48. box *box::get_next(void)
  49. {
  50.    return another_box;
  51. }
  52.  
  53.  
  54. main()
  55. {
  56. box *start = NULL;    // Always points to the start of the list
  57. box *temp;            // Working pointer
  58. box *box_pointer;     // Used for box creation
  59.  
  60.                                            // Generate the list
  61.    for (int index = 0 ; index < 10 ; index++ ) {
  62.       box_pointer = new box;
  63.       box_pointer->set(index + 1, index + 3);
  64.       if (start == NULL)
  65.          start = box_pointer;              // First element in list
  66.       else
  67.          temp->point_at_next(box_pointer); // Additional element
  68.       temp = box_pointer;
  69.    }
  70.  
  71.                                            // Print the list out
  72.    temp = start;
  73.    do {
  74.       cout << "The area is " << temp->get_area() << "\n";
  75.       temp = temp->get_next();
  76.    } while (temp != NULL);
  77.  
  78.                                            // Delete the list
  79.    temp = start;
  80.    do {
  81.       temp = temp->get_next();
  82.       delete start;
  83.       start = temp;
  84.    } while (temp != NULL);
  85. }
  86.  
  87.  
  88.  
  89.  
  90. // Result of execution
  91. //
  92. // The area is 3
  93. // The area is 8
  94. // The area is 15
  95. // The area is 24
  96. // The area is 35
  97. // The area is 48
  98. // The area is 63
  99. // The area is 80
  100. // The area is 99
  101. // The area is 120
  102.  
  103.